package com.fit.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import com.fit.interfaces.User;
import com.fit.services.SpringConfig;
@Component
public class UsersBean implements Users {

	private Map<String, User> users;
	
	private AnnotationConfigApplicationContext context;

	public UsersBean(Map<String, User> users) {
		
		this.users = users;
	}

	public UsersBean() {
		users = new HashMap<String, User>();
	}
	/* (non-Javadoc)
	 * @see com.fit.service.Users#save(com.fit.interfaces.User)
	 */
	@CommitAfter
	public User save(User user){
		return users.put(user.getUserName(), user);
	}
	
	/* (non-Javadoc)
	 * @see com.fit.service.Users#retrive()
	 */
	public Collection<User> retrive(){
		return users.values();
	}
	
	/* (non-Javadoc)
	 * @see com.fit.service.Users#retriveSingleUser(java.lang.String)
	 */
	
	public User retriveSingleUser(String userName){
		
		return users.get(userName);
	}
	
	
	
	/* (non-Javadoc)
	 * @see com.fit.service.Users#retriveSingleUser(java.io.Serializable)
	 */
	public User retriveSingleUser(Serializable id){
		return users.get(id);
	}
	
	@Override
	public User editSingleUser(User user) {
		
		return users.put(user.getUserName(), user);
	}
	
	@PostConstruct
	public void getAndSetUsers(){
		context = new AnnotationConfigApplicationContext(SpringConfig.class);
		User admin = (User) context.getBean("admin");
		User userAccount = (User) context.getBean("userAccount");
		save(admin);
		save(userAccount);
		
	}

	
	
}
